#include "material.h"
#include <GL/glut.h>

/******************************/
/* Constructeur / Destructeur */
/******************************/

Material::Material()
{
  setAmbient(0.2,0.2,0.2,1.0);
  setDiffuse(0.8,0.8,0.8,1.0);
  setSpecular(0.0,0.0,0.0,1.0);
  setEmissive(0.0,0.0,0.0,1.0);
  setShininess(0.0);
}

Material::~Material()
{
}


/*******************************/
/****** Getters / Setters ******/
/*******************************/

void Material::setAmbient(const float r, const float g, const float b, const float a)
{
  _ambient[0]=r;
  _ambient[1]=g;
  _ambient[2]=b;
  _ambient[3]=a;
}

void Material::getAmbient(float &r, float &g, float &b, float &a)
{
  r=_ambient[0];
  g=_ambient[1];
  b=_ambient[2];
  a=_ambient[3];
}

void Material::setDiffuse(const float r, const float g, const float b, const float a)
{
  _diffuse[0]=r;
  _diffuse[1]=g;
  _diffuse[2]=b;
  _diffuse[3]=a;
}

void Material::getDiffuse(float &r, float &g, float &b, float &a)
{
  r=_diffuse[0];
  g=_diffuse[1];
  b=_diffuse[2];
  a=_diffuse[3];
}

void Material::setSpecular(const float r, const float g, const float b, const float a)
{
  _specular[0]=r;
  _specular[1]=g;
  _specular[2]=b;
  _specular[3]=a;
}

void Material::getSpecular(float &r, float &g, float &b, float &a)
{
  r=_specular[0];
  g=_specular[1];
  b=_specular[2];
  a=_specular[3];
}

void Material::setEmissive(const float r, const float g, const float b, const float a)
{
  _emissive[0]=r;
  _emissive[1]=g;
  _emissive[2]=b;
  _emissive[3]=a;
}

void Material::getEmissive(float &r, float &g, float &b, float &a)
{
  r=_emissive[0];
  g=_emissive[1];
  b=_emissive[2];
  a=_emissive[3];
}

void Material::setShininess(const float shininess)
{
  _shininess=shininess;
}

float Material::getShininess()
{
  return _shininess;
}








/******************************/
/***** Fonction de dessin *****/
/******************************/

void Material::display()
{
  glMaterialfv(GL_FRONT, GL_DIFFUSE, _diffuse);
  glMaterialfv(GL_FRONT, GL_AMBIENT, _ambient);
  glMaterialfv(GL_FRONT, GL_SPECULAR, _specular);
  glMaterialfv(GL_FRONT, GL_EMISSION, _emissive);
  glMaterialf(GL_FRONT, GL_SHININESS, _shininess);
}


/******************************************/
/* MaterialCreator : materiaux predefinis */
/******************************************/

Material* MaterialCreator::createEmerald()
{
   Material *mat = new Material();
   mat->setAmbient(0.0215,0.1745,0.0215,1.0);
   mat->setDiffuse(0.07568,0.61424,0.07568,1.0);
   mat->setSpecular(0.633,0.727811,0.633,1.0);
   mat->setShininess(128*0.6);
   
   return mat;
}

Material* MaterialCreator::createJade()
{
   Material *mat = new Material();
   mat->setAmbient(0.135,0.2225,0.1575,1.0);
   mat->setDiffuse(0.54,0.89,0.63,1.0);
   mat->setSpecular(0.316228,0.316228,0.316228,1.0);
   mat->setShininess(128*0.1);
   
   return mat;
}

Material* MaterialCreator::createPearl()
{
   Material *mat = new Material();
   mat->setAmbient(0.25,0.20725,0.20725,1.0);
   mat->setDiffuse(1.0,0.829,0.829,1.0);
   mat->setSpecular(0.296648,0.296648,0.296648,1.0);
   mat->setShininess(128*0.088);
   
   return mat;
}
Material* MaterialCreator::createRuby()
{
   Material *mat = new Material();
   mat->setAmbient(0.1745,0.01175,0.01175,1.0);
   mat->setDiffuse(0.61424,0.04136,0.04136,1.0);
   mat->setSpecular(0.727811,0.626959,0.626959,1.0);
   mat->setShininess(128*0.6);
   
   return mat;
}

Material* MaterialCreator::createTurquoise()
{
   Material *mat = new Material();
   mat->setAmbient(0.1,0.18725,0.1745,1.0);
   mat->setDiffuse(0.396,0.74151,0.69102,1.0);
   mat->setSpecular(0.297254,0.30829,0.306678,1.0);
   mat->setShininess(128*0.1);
   
   return mat;
}

Material* MaterialCreator::createBronze()
{
   Material *mat = new Material();
   mat->setAmbient(0.2125,0.1275,0.054,1.0);
   mat->setDiffuse(0.714,0.4284,0.18144,1.0);
   mat->setSpecular(0.393548,0.271906,0.166721,1.0);
   mat->setShininess(128*0.2);
   
   return mat;
}

Material* MaterialCreator::createGold()
{
   Material *mat = new Material();
   mat->setAmbient(0.24725,0.1745,0.0215,1.0);
   mat->setDiffuse(0.75164,0.60648,0.22648,1.0);
   mat->setSpecular(0.628281,0.555802,0.366065,1.0);
   mat->setShininess(128*0.4);
   
   return mat;
}

Material* MaterialCreator::createSilver()
{
   Material *mat = new Material();
   mat->setAmbient(0.19225,0.19225,0.19225,1.0);
   mat->setDiffuse(0.50754,0.50754,0.50754,1.0);
   mat->setSpecular(0.508273,0.508273,0.508273,1.0);
   mat->setShininess(128*0.4);
   
   return mat;
}

Material* MaterialCreator::createChrome()
{
  Material *mat = new Material();
  mat->setAmbient(0.25,0.25,0.25,1.0);
  mat->setSpecular(0.774597,0.774597,0.774597,1.0);
  mat->setDiffuse(0.4,0.4,0.4,1.0);
  mat->setShininess(0.6*128);
  
  return mat;
}

